//
//  HWHomeSaveCardBannerCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/9.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit
import FSPagerView
import JXPageControl
class HWHomeSaveCardBannerCell: UITableViewCell {
    var banner: FSPagerView!
    lazy var codePageControl: JXPageControlJump = {
    let pageControl = JXPageControlJump(frame: CGRect(x: 0, y: 0,width: UIScreen.main.bounds.width,height: 30))
        pageControl.numberOfPages = 0
        pageControl.activeColor = .colorWithHexString(hexString: "#FF4F4F")
        pageControl.inactiveColor = .colorWithHexString(hexString: "#FFFFFF")
        pageControl.activeSize = CGSize(width: 9*widthScale, height: 4*widthScale)
        pageControl.inactiveSize = CGSize(width: 9*widthScale, height: 4*widthScale)
        pageControl.columnSpacing = 4*widthScale

        return pageControl
    }()
    var models:[HWHomeMainSlideModel]? {
        didSet {
            codePageControl.numberOfPages = models?.count ?? 0
            banner.reloadData()
        }
    }
    var didClickItem:((_:HWHomeMainSlideModel)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWHomeSaveCardBannerCell) {
        let reUse:String = "HWHomeSaveCardBannerCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeSaveCardBannerCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeSaveCardBannerCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.colorWithHexString(hexString: "#FFFFFF")
        
        banner = FSPagerView()
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.backgroundColor = .clear
        banner.register(HWHomeSaveCardBannerPagerCell.self, forCellWithReuseIdentifier: "HWHomeSaveCardBannerPagerCell")
        banner.automaticSlidingInterval = 3
        banner.removesInfiniteLoopForSingleItem = true
        contentView.addSubview(banner)
        
        contentView.addSubview(codePageControl)
    }
    
    func initFrame() -> Void {
        banner.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(77*widthScale)
        }
        
        codePageControl.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.bottom.equalTo(banner.snp.bottom).offset(-5*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWHomeSaveCardBannerCell:FSPagerViewDelegate,FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return models?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HWHomeSaveCardBannerPagerCell", at: index) as! HWHomeSaveCardBannerPagerCell
        cell.model = models?[index]
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        self.didClickItem?(models?[index] ?? HWHomeMainSlideModel())
    }
    
    func pagerViewDidScroll(_ pagerView: FSPagerView) {
        self.codePageControl.progress = pagerView.scrollOffset
    }
    
}

class HWHomeSaveCardBannerPagerCell:FSPagerViewCell {
    var bgImageView:UIImageView!
    
    var model:HWHomeMainSlideModel? {
        didSet {
            bgImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        
        contentView.layer.shadowColor = UIColor.clear.cgColor
        bgImageView = UIImageView()
        bgImageView.layer.masksToBounds = true
        bgImageView.backgroundColor = .clear
        bgImageView.contentMode = .scaleAspectFill
        contentView.addSubview(bgImageView)
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(12*widthScale)
            make.right.equalTo(-12*widthScale)
            make.height.equalTo(77*widthScale)
        }
    }
}
